package com.landmesure.settings.fragment.argsinfo;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.code.EntityType;
import com.code.EnumValue;
import com.code.PropertyDefinition;
import com.landmesure.bluetoothchat.LandCommunicationService;
import com.landmesure.bluetoothchat.constant.Constant;
import com.landmesure.bluetoothchat.view.BluetoothFragment;
import com.landmesure.settings.fragment.tree.TypeTree;
import com.landmesure.settings.fragment.tree.TypeTreeNote;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("unchecked")
public class DataHelper {
    private static final String TAG = "DataHelper";

    private TypeTree mTree;
    private LandCommunicationService mService;
    private ArrayList<EntityType> mEntityTypes;
    private Handler mHandler;

    public DataHelper() {
        mTree = new TypeTree();
        mService = BluetoothFragment.landCommunicationServiceInstance;

        mHandler = mService.getHandler();
        createTree(mService.getData());
    }

    /**
     * 设置参数
     * @param argValues 被设置参数的参数编码的集合
     */
    public void setEntityType(TypeTreeNote note,ArrayList<String> argValues) {
        Log.i(TAG, "setEntityType");
        EntityType type = null;
        for (EntityType t : mEntityTypes) {
            if (t.getEcode() == note.getId()) {
                type = t;
                break;
            }
        }
        if (null == type) {
            return;
        }

        ArrayList<ArgumentsInfo> infos = note.getArgumentsInfos();
        ArrayList<String> values = (ArrayList<String>) argValues.clone();
        final int size = infos.size();
        ArgumentsInfo info;
        List<PropertyDefinition> definitions = type.getPropertyDefinitions();
        List<EnumValue> enumValues;
        PropertyDefinition definition;
        for (int i = 0; i < size; i++) {
            info = infos.get(i);
            if (values.contains(info.mArguValue)) {
                definition = definitions.get(i);
                if (definition.isEnumable()) {
                    enumValues = definition.getEnumValues();
                    for (EnumValue value : enumValues) {
                        if (info.mSelectStr.equals(value.getValue())) {
                            definition.setValue(value.getCode());
                        }
                    }
                } else {
                    definition.setValue(info.mPropValue);
                }
            }
        }
        mService.setArgs(type, values);
        mHandler.sendEmptyMessage(Constant.SETUP_PARAMETERS);
    }

    /**
     * 选测
     */
    public void choiceTest(int index) {
        Message message = Message.obtain();
        message.what = Constant.SELECT_TEST;
        message.obj = mEntityTypes.get(index);
        mHandler.sendMessage(message);
    }

    /**
     * 查询参数
     */
    public void queryArgs() {
        mHandler.sendEmptyMessage(Constant.QUERY_MEASUREMENT_BOARD);
    }

    /**
     * 复位
     */
    public void reset() {
        Message message = Message.obtain();
        message.what = Constant.RESET;
        message.obj = mEntityTypes.get(0);
        mHandler.sendMessage(message);
    }

    private void createTree(ArrayList<EntityType> entityTypes) {
        if (null == entityTypes || entityTypes.isEmpty()) {
            Log.e(TAG, "createTree null pointer");
            return;
        }
        mEntityTypes = entityTypes;

        final int size = entityTypes.size();
        ArrayList<EntityType> cengs = new ArrayList<>();    //储存同一层的节点
        ArrayList<EntityType> cClons = null;    //克隆同一层的节点
        boolean isFirst = false;    //是否为根节点
        int level = 0;    //层级数
        EntityType type;
        for (int i = 0; i < size; i++) {
            type = entityTypes.get(i);
            if (0 > type.getFcode()) {
                addNote(type, level, cengs);
                isFirst = true;
                continue;
            }
            if (isFirst) {
                isFirst = false;
                cClons = clones(cengs);
                level++;
            }

            boolean isgo = false;
            if (null == cClons) {
                continue;
            }
            for (EntityType et : cClons) {
                if (type.getFcode() == et.getEcode()) {
                    addNote(type, level, cengs);
                    isgo = true;
                }
            }

            if (!isgo) {
                level++;
                cClons = clones(cengs);
                addNote(type, level, cengs);
            }
        }
    }

    private void addNote(EntityType type, int level, ArrayList<EntityType> cengs) {
        mTree.addNote(type.getEcode(), type.getFcode(),
                level, type.getName(), type.copySelf());
        cengs.add(type);
    }

    private ArrayList<EntityType> clones(ArrayList<EntityType> cengs) {
        ArrayList<EntityType> cClons = (ArrayList<EntityType>) cengs.clone();
        cengs.clear();
        return cClons;
    }

    public TypeTree getTree() {
        mTree.traversalTree(mTree.buildTree(mTree.getNotes()));
        Log.i(TAG, "size = " + mTree.getAllNotes().size());
        return (TypeTree) mTree.clone();
    }

    public ArrayList<EntityType> getEntityTypes() {
        if (null == mEntityTypes) {
            mEntityTypes = new ArrayList<>();
        }
        return mEntityTypes;
    }
}
